package com.yxkj.syh.app.huarong.activities.orders.detail.status0;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.syh.app.basic.base.BaseActivity;
import com.syh.app.basic.utils.AppUtil;
import com.syh.app.basic.utils.Tooast;
import com.yxkj.syh.app.huarong.BR;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.activities.creat.edit.EditOrderActivity;
import com.yxkj.syh.app.huarong.adps.AddedGoodsAdp;
import com.yxkj.syh.app.huarong.bean.Goods;
import com.yxkj.syh.app.huarong.constants.ArouterPath;
import com.yxkj.syh.app.huarong.databinding.ActivityOrderWaitBinding;

import java.util.List;

/**
 * 待发货
 */
@Route(path = ArouterPath.ORDER_WAIT_ACTIVITY)
public class OrderWaitActivity extends BaseActivity<ActivityOrderWaitBinding, OrderWaitVM> {

    @Autowired
    long orderId;

    AddedGoodsAdp mAddedGoodsAdp;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_order_wait;
    }

    @Override
    protected int getViewModelId() {
        return BR.orderWaitVM;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        if (orderId <= 0) finish();

        mVDBinding.titleView.setLeftOnClickListener(v -> finish());

        mAddedGoodsAdp = new AddedGoodsAdp();
        mVDBinding.rvGoods.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        mVDBinding.rvGoods.setAdapter(mAddedGoodsAdp);
        mAddedGoodsAdp.setEditAble(false);

        mVDBinding.btnConfirm.setOnClickListener(v -> {
            if (mViewModel.mldOrderDetail.getValue() != null) {
                Intent intent = new Intent(OrderWaitActivity.this, EditOrderActivity.class);
                intent.putExtra("orderInfo", mViewModel.mldOrderDetail.getValue());
                startActivityForResult(intent, 0x3a);
            }
        });
        mVDBinding.btnCancel.setOnClickListener(v -> {
            if (mViewModel.mldOrderDetail.getValue() == null) return;
            new AlertDialog.Builder(OrderWaitActivity.this).setMessage(mViewModel.mldOrderDetail.getValue().getNo()+" 确认取消该运货单？")
                    .setNegativeButton("不",null)
                    .setPositiveButton("确认", (dialog, which) -> {
                        dialog.cancel();
                        mViewModel.cancelOrder();
                    }).create().show();

        });

        mVDBinding.tvCopyNo.setOnClickListener(v -> {
            if (AppUtil.copy2Clipboard(OrderWaitActivity.this, mViewModel.mldOrderDetail.getValue().getNo())) {
                Tooast.success("运单编号已复制到粘贴板");
            }
        });

    }

    @Override
    protected void initData() {
        mViewModel.getOrderDetail(orderId);
    }

    @Override
    protected void initObservers() {
        super.initObservers();
        mViewModel.mldOrderDetail.observe(this, orderInfo -> {
            setGoods(orderInfo.getOrderProductList());
        });

    }

    private void setGoods(List<Goods> goods) {
        for (Goods temp : goods) {
            temp.setAdded(true);
            temp.setLableStatus(0);
            temp.setQuantity(temp.getWeight());
        }
        mAddedGoodsAdp.setNewData(goods);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 0x3a) {//发货成功
                finish();
            }
        } else {
            if (requestCode == 0x3a) {//未发货  但可能修改了订单信息
                mViewModel.getOrderDetail(orderId);
            }
        }
    }

}
